package com.bjsxt.front.portal.service.impl;

import com.bjsxt.front.portal.feign.CommonFeignClient;
import com.bjsxt.front.portal.feign.CommonRedisFeignClient;
import com.bjsxt.front.portal.service.ItemService;
import com.bjsxt.pojo.TbItem;
import com.bjsxt.pojo.TbItemDesc;
import com.bjsxt.pojo.TbItemParamItem;
import com.bjsxt.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 查询商品业务层
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private CommonFeignClient commonFeignClient;

    @Autowired
    private CommonRedisFeignClient commonRedisFeignClient;
    @Override
    public Result selectItemInfo(Long itemId) {
        //查询缓存
        try {
            TbItem tbItem = this.commonRedisFeignClient.selectItemBasicInfo(itemId);
            if(tbItem!=null){
                return Result.ok(tbItem);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //查询数据库
        TbItem tbItem = this.commonFeignClient.selectItemInfo(itemId);
        try {
           if(tbItem!=null){
               this.commonRedisFeignClient.insertItemBasicInfo(tbItem);
           }
        }catch (Exception e){
            e.printStackTrace();
        }
        if(tbItem!=null){
            return Result.ok(tbItem);
        }
        return Result.error("查无结果");
    }

    /**
     * 查询商品介绍
     * @param itemId
     * @return
     */
    @Override
    public Result selectItemDescByItemId(Long itemId) {
        //查询缓存
        try {
            TbItemDesc tbItemDesc = this.commonRedisFeignClient.selectItemDesc(itemId);
            if(tbItemDesc!=null){
                return Result.ok(tbItemDesc);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //查询数据库
        TbItemDesc tbItemDesc = this.commonFeignClient.selectItemDescByItemId(itemId);
        try {
            if(tbItemDesc!=null){
                this.commonRedisFeignClient.insertItemDesc(tbItemDesc);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        if(tbItemDesc!=null){
            return Result.ok(tbItemDesc);
        }
        return Result.error("查无结果");
    }

    /**
     * 根据商品ID查询商品规格参数
     * @param itemId
     * @return
     */
    @Override
    public Result selectTbItemParamItemByItemId(Long itemId) {
        //查询缓存
        try {
            TbItemParamItem tbItemParamItem = this.commonRedisFeignClient.selectItemParamItem(itemId);
            if(tbItemParamItem!=null){
                return Result.ok(tbItemParamItem);
            }
        }catch (Exception e){
           e.printStackTrace();
        }
        //查询数据库
        TbItemParamItem tbItemParamItem = this.commonFeignClient.selectTbItemParamItemByItemId(itemId);
        try {
            if(tbItemParamItem!=null){
                this.commonRedisFeignClient.insertItemParamItem(tbItemParamItem);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        if(tbItemParamItem!=null){
            return Result.ok(tbItemParamItem);
        }
        return Result.error("查无结果");
    }
}
